package com.wlcorp.forcazamis.business.sample.impl;

import java.util.List;

import com.wlcorp.forcazamis.business.sample.ISampleService;
import com.wlcorp.forcazamis.dao.sample.ISampleDAO;
import com.wlcorp.forcazamis.dao.sample.ISampleImageDAO;
import com.wlcorp.forcazamis.dao.sample.ISubSampleAssignDAO;
import com.wlcorp.forcazamis.dao.sample.ISubSampleDAO;
import com.wlcorp.forcazamis.domain.sample.Sample;
import com.wlcorp.forcazamis.domain.sample.SampleImage;
import com.wlcorp.forcazamis.domain.sample.SubSample;
import com.wlcorp.forcazamis.domain.sample.SubSampleAssign;
import com.wlcorp.forcazamis.exception.ServiceException;
import com.wlcorp.forcazamis.sys.BaseService;
import com.wlcorp.forcazamis.sys.util.AppConstant;

public class SampleServiceImpl extends BaseService implements ISampleService {
	
	private ISampleDAO sampleDAO;
	private ISubSampleDAO subSampleDAO;
	private ISampleImageDAO sampleImageDAO;
	private ISubSampleAssignDAO subSampleAssignDAO;
	public List<Sample> getSampleList(Sample sample) throws ServiceException {
		return sampleDAO.getSampleList(sample);
	}

	public Long getSampleCount(Sample sample) throws ServiceException {
		return sampleDAO.getSampleCount(sample);
	}

	public Sample getSampleById(Long sampleId) throws ServiceException {
		if(null == sampleId){
			return null;
		}
		return sampleDAO.getSampleById(sampleId);
	}

	public void deleteSample(Sample sample) throws ServiceException {
		sample.setFlag(AppConstant.RECORD_DELETED);
		sampleDAO.saveSample(sample);
	}

	public void saveSample(Sample sample) throws ServiceException {
		//新增的时候判断编码是否重复，因为算号器的原因，会出现并发重复
		if(null == sample.getSampleId()){
			List<Sample> sampleList = sampleDAO.getSampleByCode(sample);
			if(sampleList.size() > 0){
				setErrorField(sample,AppConstant.BASE_BEAN_STATUS_ERROR,"编码重复，请重新输入编码！");
				return;
			}
		}
		sampleDAO.saveSample(sample);
	}

	public void setSampleDAO(ISampleDAO sampleDAO) {
		this.sampleDAO = sampleDAO;
	}
	
	public void setSubSampleDAO(ISubSampleDAO subSampleDAO) {
		this.subSampleDAO = subSampleDAO;
	}
	
	public void setSampleImageDAO(ISampleImageDAO sampleImageDAO) {
		this.sampleImageDAO = sampleImageDAO;
	}
	
	public Long getSameTypeNewSerialCode(Sample sample) throws ServiceException {
		
		Long serialCode = sampleDAO.getSameTypeLatestSerialCode(sample);
		return serialCode + 1;
	}

	public void setSubSampleAssignDAO(ISubSampleAssignDAO subSampleAssignDAO) {
		this.subSampleAssignDAO = subSampleAssignDAO;
	}

	public void saveSubSample(SubSample subSample) throws ServiceException {
		Sample parentSample = sampleDAO.getSampleById(subSample.getParentSampleId());
		subSample.setParentSample(parentSample);
		subSampleDAO.saveSubSample(subSample);
	}
	
	public void saveSampleImage(SampleImage sampleImage) throws ServiceException {
		Sample parentSample = sampleDAO.getSampleById(sampleImage.getParentSampleId());
		sampleImage.setParentSample(parentSample);
		sampleImageDAO.saveSampleImage(sampleImage);
	}

	public void deleteSubSample(SubSample subSample) throws ServiceException {
		SubSampleAssign subSampleAssign = new SubSampleAssign();
		subSampleAssign.setSubSampleId(subSample.getSubSampleId());
		subSampleAssign = subSampleAssignDAO.getSubSampleAssignBySubSampleAndOwner(subSampleAssign);
		if(null != subSampleAssign){
			setErrorField(subSample,AppConstant.BASE_BEAN_STATUS_ERROR,"该样品已使用，不能删除！");
			return;
		}
		subSample.setFlag(AppConstant.RECORD_DELETED);
		subSampleDAO.saveSubSample(subSample);
	}

	public void deleteSampleImage(Long sampleImageId) throws ServiceException {
		SampleImage sampleImage = sampleImageDAO.getSampleImageById(sampleImageId);
		sampleImageDAO.deleteSampleImage(sampleImage);
	}

	public List<SubSample> getSubSampleList(SubSample subSample) throws ServiceException {
		return subSampleDAO.getSubSampleList(subSample);
	}

	public Long getSubSampleCount(SubSample subSample) throws ServiceException {
		return subSampleDAO.getSubSampleCount(subSample);
	}

	public SubSample getSubSampleById(Long subSampleId) throws ServiceException {
		if(null == subSampleId){
			return null;
		}
		return subSampleDAO.getSubSampleById(subSampleId);
	}

	public List<Sample> getSampleByCode(Sample sample) throws ServiceException {
		return sampleDAO.getSampleByCode(sample);
	}

//	public List getSubSampleAssignList(SubSampleAssign subSampleAssign) throws ServiceException {
//		return subSampleAssignDAO.getSubSampleAssignList(subSampleAssign);
//	}
	
	
}
